package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserHouseBar;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserHouseBarDaoImpl extends TempDataObjectDao<UserHouseBar> {

    public Map<Integer,UserHouseBar> getUserHouseBarMap(long userId) {
        Map<Integer,UserHouseBar> resMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_house_bar where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHouseBar data = parse(rs);
                resMap.put(data.getBarPos(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resMap;
    }

    @Override
    protected boolean addData(UserHouseBar data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_house_bar(userId,barPos,patronsId,lastRewardTime,surplusTime,openTime,dispatchTime,toadSilver) values (?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBarPos());
            call.setInt(i++, data.getPatronsId());
            call.setLong(i++, data.getLastRewardTime());
            call.setLong(i++, data.getSurplusTime());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getDispatchTime());
            call.setString(i++, data.getToadSilver());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHouseBar data) {
        PreparedStatement call = null;
        String sql = "update t_u_house_bar set patronsId=?,lastRewardTime=?,surplusTime=?,dispatchTime=?,toadSilver=? where userId=? AND barPos = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getPatronsId());
            call.setLong(i++, data.getLastRewardTime());
            call.setLong(i++, data.getSurplusTime());
            call.setLong(i++, data.getDispatchTime());
            call.setString(i++, data.getToadSilver());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBarPos());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHouseBar parseData(ResultSet rs) throws SQLException {
        UserHouseBar data = new UserHouseBar();
        data.setUserId(rs.getLong("userId"));
        data.setBarPos(rs.getInt("barPos"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setLastRewardTime(rs.getLong("lastRewardTime"));
        data.setSurplusTime(rs.getLong("surplusTime"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setDispatchTime(rs.getLong("dispatchTime"));
        data.setToadSilver(rs.getString("toadSilver"));

        return data;
    }
}
